package com.goodflower.goodflower;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;

import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.goodflower.goodflower.chang_user.Change_user_password;
import com.goodflower.goodflower.chang_user.Change_user_phone;
import com.goodflower.goodflower.chang_user.Change_user_touxiang;
import com.goodflower.goodflower.creat_flower.Creat_flower_one;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.goodflower.goodflower.R.drawable.ic_home;

public class FlowerSelect extends AppCompatActivity {
    private TextView flower_title;
    private Button btn_home;
    private DrawerLayout drawerLayout;
    private FlowerItemAdapter flowerItemAdapter;
    private List<FlowerItem> flowerItem_List = new ArrayList<>();
    private SwipeRefreshLayout swipeRefresh;

    private String mac;
    private String flower_name;
    private String username;

    private FloatingActionButton fab;

    private void refreshflowerItem() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getflowerItem();
            }
        }).start();
    }

    private void getflowerItem() {
        flowerItem_List.clear();
        HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/getfloweritem.php?mac=" + mac, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String flower_item = response.body().string();
                String[] all_flower_item = flower_item.split("\\|");
                int id = 0;
                for (String item : all_flower_item) {
                    id++;
                    if (!item.equals("#")) {
                        FlowerItem flowerItem = new FlowerItem(item, mac, username, id);
                        flowerItem_List.add(flowerItem);
                    }

                }
                if (swipeRefresh != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            flowerItemAdapter.notifyDataSetChanged();
                            swipeRefresh.setRefreshing(false);
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flower);
        final Intent intent = getIntent();
        mac = intent.getStringExtra("mac");
        flower_name = intent.getStringExtra("flower_name");
        username = intent.getStringExtra("username");
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        final ImageView icon = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.icon_image);
        Glide.with(getApplicationContext()).load("http://"+Info.server_ip+"/huahua/userpic/" + username + ".jpg").signature(new StringSignature(UUID.randomUUID().toString())).into(icon);

        TextView nav_name = (TextView) navigationView.getHeaderView(0).findViewById(R.id.usr_name);
        TextView nav_phone = (TextView)navigationView.getHeaderView(0).findViewById(R.id.usr_phone);
        nav_name.setText(Info.current_user_name);
        nav_phone.setText(Info.current_user_phone);


        navigationView.setCheckedItem(R.id.user_change_phone);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                drawerLayout.closeDrawers();
                switch (item.getTitle().toString()){
                    case "退出当前用户":
                        SharedPreferences.Editor editor = getSharedPreferences("login_date", MODE_PRIVATE).edit();
                        editor.putString("login_name","");
                        editor.apply();
                        Intent logoff_intent = new Intent(FlowerSelect.this, MainActivity.class);
                        startActivity(logoff_intent);
                        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                        break;
                    case "更改密码":
                        Intent change_password_intent = new Intent(FlowerSelect.this, Change_user_password.class);
                        startActivity(change_password_intent);
                        break;
                    case "更换用户头像":
                        startActivity(new Intent(FlowerSelect.this, Change_user_touxiang.class));
                        break;
                    case "修改绑定手机号":
                        startActivity(new Intent(FlowerSelect.this, Change_user_phone.class));
                        break;
                }
                return  true;
            }
        });


        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            flower_title = (TextView) findViewById(R.id.title_flower);
            flower_title.setText(flower_name);
            btn_home = (Button) findViewById(R.id.nav_button);
            btn_home.setBackgroundResource(R.drawable.ic_back);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            btn_home.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // drawerLayout.openDrawer(GravityCompat.START);
                    Intent intent = new Intent(FlowerSelect.this, FlowerActivity.class);
                    intent.putExtra("yourname", Info.current_user_name);
                    startActivity(intent);
                    finish();
                    overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                }
            });
            getflowerItem();
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
            GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
            recyclerView.setLayoutManager(layoutManager);
            flowerItemAdapter = new FlowerItemAdapter(flowerItem_List);
            recyclerView.setAdapter(flowerItemAdapter);
            swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
            swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
            swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refreshflowerItem();
                }
            });
            fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent creat_flower_intent = new Intent(FlowerSelect.this, Creat_flower_one.class);
                    creat_flower_intent.putExtra("mac_mac", mac);
                    creat_flower_intent.putExtra("mac_flower_name", flower_name);
                    startActivity(creat_flower_intent);
                }
            });
        }


    }
}
